package com.quectel.core.module.device.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.core.module.device.dao.DeviceVendorDao;
import com.quectel.core.module.device.dto.DeviceVendorDto;
import com.quectel.core.module.device.entity.DeviceVendorEntity;
import com.quectel.core.module.device.service.DeviceVendorService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.redis.RedisUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.quectel.core.constants.RedisCacheConstants.PROJECT_NAME;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-06-03 09:55:24
 */
@DubboService
public class DeviceVendorServiceImpl implements DeviceVendorService {
    /**
     * 设备厂商缓存 id==>>DeviceVendorDto
     */
    public static final String DEVICE_VENDOR_CACHE = PROJECT_NAME + "DEVICE_VENDOR_CACHE";
    @Autowired
    private DeviceVendorDao deviceVendorDao;

    @Override
    public DeviceVendorDto selectCacheById(Long id) {

        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEVICE_VENDOR_CACHE + id,
                5 * 60
        );
    }

    @Override
    public DeviceVendorDto selectByCode(String code) {

        DeviceVendorEntity deviceVendorEntity = deviceVendorDao.selectOne(new QueryWrapper<DeviceVendorEntity>().eq("code", code)
                .last(SqlConstants.MYSQL_LIMIT_ONE));

        DeviceVendorDto deviceVendorDto = CopyUtils.copyObj(deviceVendorEntity, DeviceVendorDto.class);


        return deviceVendorDto;
    }


    @Override
    public List<DeviceVendorDto> queryList(Map<String, Object> params) {
        List<DeviceVendorEntity> list = deviceVendorDao.queryList(params);
        return CopyUtils.copyList(list, DeviceVendorDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deviceVendorDao.queryTotal(params);
    }

    @Override
    public Long save(DeviceVendorDto dto) {
        DeviceVendorEntity entity = CopyUtils.copyObj(dto, DeviceVendorEntity.class);
        entity.setId(Snowflake.nextId());
        deviceVendorDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeviceVendorDto dto) {

        RedisUtils.del(DEVICE_VENDOR_CACHE + dto.getId());

        DeviceVendorEntity entity = CopyUtils.copyObj(dto, DeviceVendorEntity.class);
        deviceVendorDao.updateById(entity);
    }

    @Override
    public DeviceVendorDto selectById(Long id) {
        DeviceVendorEntity entity = deviceVendorDao.selectById(id);
        DeviceVendorDto deviceVendorDto = CopyUtils.copyObj(entity, DeviceVendorDto.class);
        return deviceVendorDto;
    }

    @Override
    public void deleteById(Long id) {

        RedisUtils.del(DEVICE_VENDOR_CACHE + id);

        deviceVendorDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
